//
//  BanBu_FriendListCell.m
//  BanBu
//
//  Created by Wang Xj on 13-8-22.
//
//

#import "BanBu_FriendListCell.h"
#define x 60

@implementation BanBu_FriendListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(-40, 53, 360, 1.0)];
        lineView.backgroundColor = [UIColor colorWithRed:224.0/255 green:224.0/255 blue:224.0/255 alpha:1.0];
        [self.contentView addSubview:lineView];
        [lineView release];
        
        BanBu_ImageView *backImage = [[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeCell];
        backImage.frame = CGRectMake(0, 0, 320, 58);
        self.backgroundView = backImage;
        [backImage release];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5,5, 44, 44)];
        _iconView = imageView;
        _iconView.layer.cornerRadius = 22.0;
        _iconView.layer.masksToBounds = YES;
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
        
		[self.contentView addSubview:_iconView];
        [imageView release];
        
		self.nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(x,17, 140, 20)] autorelease];
		self.nameLabel.textAlignment = kTextAlignmentLeft;
		self.nameLabel.backgroundColor = [UIColor clearColor];
		self.nameLabel.font = [UIFont systemFontOfSize:16];
		[self.contentView addSubview:self.nameLabel];

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void) setEditing:(BOOL)editting animated:(BOOL)animated
{
	if (self.editing == editting)
	{
		return;
	}
	
	[super setEditing:editting animated:animated];
	
	if (editting)
	{
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		self.textLabel.backgroundColor = [UIColor clearColor];
		self.detailTextLabel.backgroundColor = [UIColor clearColor];
		
		if (self.m_checkImageView == nil)
		{
			self.m_checkImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellNotSelected.png"]] autorelease];
			[self addSubview:self.m_checkImageView];
		}
		
		[self setChecked:self.m_checked];
 		self.m_checkImageView.center = CGPointMake(-CGRectGetWidth(self.m_checkImageView.frame) * 0.5,
											  CGRectGetHeight(self.bounds) * 0.5);
		self.m_checkImageView.alpha = 0.0;
		[self setCheckImageViewCenter:CGPointMake(20.5, CGRectGetHeight(self.bounds) * 0.5)
								alpha:1.0 animated:animated];
	}
	else
	{
		self.m_checked = NO;
		self.selectionStyle = UITableViewCellSelectionStyleBlue;
		
		if (self.m_checkImageView)
		{
			[self setCheckImageViewCenter:CGPointMake(-CGRectGetWidth(self.m_checkImageView.frame) * 0.5,
													  CGRectGetHeight(self.bounds) * 0.5)
									alpha:0.0
								 animated:animated];
		}
	}
}

//多选删除部分
- (void) setCheckImageViewCenter:(CGPoint)pt alpha:(CGFloat)alpha animated:(BOOL)animated
{
	if (animated)
	{
		[UIView beginAnimations:nil context:nil];
		[UIView setAnimationBeginsFromCurrentState:YES];
		[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
		[UIView setAnimationDuration:0.3];
		
		self.m_checkImageView.center = pt;
		self.m_checkImageView.alpha = alpha;
		
		[UIView commitAnimations];
	}
	else
	{
		self.m_checkImageView.center = pt;
		self.m_checkImageView.alpha = alpha;
	}
}

//设置选中的图标
- (void) setChecked:(BOOL)checked
{
	if (checked)
	{
		self.m_checkImageView.image = [UIImage imageNamed:@"CellSelected.png"];
 	}
	else
	{
		self.m_checkImageView.image = [UIImage imageNamed:@"CellNotSelected.png"];
 	}
	self.m_checked = checked;
}

//设置用户头像
- (void)setAvatar:(NSString *)avatarUrlStr
{
    [self.iconView setImageWithURL:[NSURL URLWithString:avatarUrlStr] placeholderImage:[UIImage imageNamed:@"default.png"] andType:1];
}

//设置用户昵称
- (void)setName:(NSString *)name
{
    self.nameLabel.text = name;
}

-(void)dealloc
{
    [_nameLabel release];
    [_m_checkImageView release];
    
    [super dealloc];
}

@end
